package main

import (
	"fmt"
	"sync"
)

type Sequence[E any] struct {
	elements []E
	lock     sync.Mutex
}

// 指定接收者的函数中只能有接收者携带的泛型信息，不能再单独指定泛型信息
func (seq *Sequence[E]) Push(element E) {
	defer seq.lock.Unlock()
	seq.lock.Lock()
	seq.elements = append(seq.elements, element)
}

func (seq *Sequence[E]) Pop() E {
	defer seq.lock.Unlock()
	seq.lock.Lock()

	var result E
	if len(seq.elements) == 0 {
		return result
	}
	result = seq.elements[0]
	seq.elements = seq.elements[1:]
	return result
}

func main() {
	seq1 := &Sequence[string]{}
	seq1.Push("A")
	fmt.Println(seq1.Pop())

	seq2 := &Sequence[int]{}
	seq2.Push(2)
	fmt.Println(seq2.Pop())
}
